<?php
/**
 * Text element
 *
 */
class ArrowFormImageupload extends ArrowFormField{
	
	protected $properties = array(
		"require" => false,
		"alertSize" => false,
		"alertRequire" => false,
		"alertType" => false,	
		"accepted" => array( "jpg", "jpeg", "png"),
		"size" => 3 //Mb
		
	);
	
	public function validate( $proposedValue ){
		if( $this->isCheckable() ){
			$name = $this->getName();
			
			
			if( !isset($_FILES[$name]) && $this->getProperty( "require" ) != false )
				return $this->getProperty( "alertRequire" );
				
			if( isset($_FILES[$name] ) ){

				if(  $_FILES[$name]["size"] == 0 && $this->getProperty( "require" ) != false )
					return $this->getProperty( "alertRequire" );
					
				if( number_format($_FILES[$name]["size"]/(1024*1024),1) > $this->getProperty("size")  )
					return $this->getProperty( "alertSize" );
									
				$accepted = $this->getProperty("accepted");
				if( !is_array($accepted) ){
					$this->properties["accepted"] = explode("|", $accepted);
				}								
									
				$files =  explode( '.', strtolower($_FILES[$name]['name'] ) );
				$files = array_pop($files);
				
				 
				if( !empty($_FILES[$name]['name'] ) && !in_array( $files, $this->getProperty('accepted')) )
					return $this->getProperty( "alertType" );
				
			}
		}
		return true;
	}
	
	
	public function generateOutput(){
		$attribuites = array();
		$attribuites["name"] = $this->getName();
		$str = '<input type="file" '.$this->genTagsFromArray($attribuites).' '.$this->addExternal().'/>';
		$value = $this->getValue(); 
		if( !empty($value) ){
			$str.= '<input type="checkbox" name="data[resourceR][]" value="'.$attribuites["name"].'" /> Usuń <br />';
			$str.= '<img src="'.$value.'" style="width:300px;" />';
		}
		return $str;
	}


}
?>